<?php

namespace app\h5api\controller;

use app\common\model\User;
use think\facade\Config;

class Place extends Base
{
    /**
     * @route('h5/GoToPlace')
     * 购物车下单
     */
    public function GoToPlace()
    {
        $ids = input('post.cart_ids');
        $receive_id = input('post.receive_id');
        $address_id = input('post.address_id');
        $pay_end = input('post.pay_end', 1);
        $CartModel = new \app\common\model\Cart();
        $AddressModel = new \app\common\model\Address();
        $OrderModel = new \app\common\model\Order();
        $user = $this->GetUserInfo();
//        $user['wx_openid'] = $this->_openid;
//        var_dump($user);die;
        $address = $AddressModel->checkaddress($user['user_id'], $address_id);
        if (!$address) {
            $this->result([], -1, '用户不存在的收货地址');
        }
        $data = $CartModel->Settlement($user, $ids, $receive_id);
//		var_dump($data);die;
        if (!$data) {
            $this->result([], -1, $CartModel->getError());
//			$this->result([],-1,'商铺或者商品已下架');
        }
//		var_dump($data['price']['pay_amount']);die;
        $createorders = $OrderModel->placecreateorder($user, $ids, $data['price'], $address_id, $receive_id);
//		var_dump($createorders);die;
        if ($createorders['code'] === false) {
            $this->result([], -1, $OrderModel->getError());
        }
//		var_dump($createorders);
        if ($pay_end == 1) {
            $wxconfig = Config::pull('weixin');
        } else {
            $wxconfig = Config::pull('weixin_applet');
        }
//		var_dump($wxconfig);die;
        $notify = "/h5/notify";
        $rs = $OrderModel->wxpay($user, $createorders['data']['paymentsid'], $wxconfig, $notify, $pay_end);
//        var_dump($rs);die;
        if ($rs === false) {
            $this->result([], -1, $OrderModel->getError());
        }
        $rs['data']['payments_id'] = $createorders['data']['paymentsid'];
        $rs['data']['order_id'] = $createorders['data']['orders'][0];
//        $rs['data']['order_ids'] = $createorders['data']['orders'];
        $rs['data']['is_pay'] = false;
        $this->result($rs, 1, '操作成功');
    }


    /**
     * @route('h5/UncommonPlace')
     * 兑换商品下单
     */
    public function UncommonPlace()
    {
        $product_id = input('post.product_id');
        $stock_id = input('post.stock_id');
        $quantity = input('post.quantity');
        $receive_id = input('post.receive_id');
        $address_id = input('post.address_id');
        $AddressModel = new \app\common\model\Address();
        $OrderModel = new \app\common\model\Order();
        $quantity = empty($quantity) ? "1" : $quantity;
        $user = $this->GetUserInfo();
        $data = $OrderModel->checkplace($user, $product_id, $stock_id, $quantity, $receive_id);
        if ($data === false) {
            $this->result([], -1, $OrderModel->getError());
        }

        if ($data['productinfo']['show'] == 4) {
            $address = $AddressModel->checkaddress($user['user_id'], $address_id);
            if (!$address) {
                $this->result([], -1, '用户不存在的收货地址');
            }
            $rs = $OrderModel->placeexchangeorder($user, $data, $quantity, $address);

            if ($rs === false) {
                $this->result([], -1, $OrderModel->getError());
            }
            $this->result('', 1, '兑换成功');
        }
    }


    /**
     * @route('h5/ToBePaid')
     * 订单待支付去支付
     */
    public function ToBePaid()
    {
        $orderModel = new \app\common\model\Order();
        $user = $this->GetUserInfo();
        $pay_end = input('post.pay_end', 1);
        $where = [
            'user_id' => $user['user_id'],
            'order_id' => input('post.order_id'),
        ];
        $thecancel = $orderModel->tobepaidordercheck($where);
        if ($thecancel === false) {
            $this->result([], -1, $orderModel->getError());
        }

        $result['data']['is_can_pay'] = false;
        switch ($thecancel['code']) {
            case 2:
                $rs = $orderModel->updateorderandstock($thecancel['data']['details'], $thecancel['data']['order']);
                if ($rs === false) {
                    $this->result($result, -1, $orderModel->getError());
                }
                $this->result($result, 1, $thecancel['msg'] . "已取消订单");
                break;
            case 1:
                if ($pay_end == 1) {
                    $wxconfig = Config::pull('weixin');
                } else {
                    $wxconfig = Config::pull('weixin_applet');
                }
                $notify = "/h5/notify";
                $rs = $orderModel->wxpay($user, $thecancel['data']['order']['payments_id'], $wxconfig, $notify,$pay_end);
                if ($rs === false) {
                    $this->result($result, -1, $orderModel->getError());
                }
                $result['data'] = $rs;
                $result['data']['payments_id'] = $thecancel['data']['order']['payments_id'];
                $result['data']['order_id'] = $thecancel['data']['order']['order_id'];
                $result['data']['is_can_pay'] = true;
                $this->result($result, 1, '操作成功');
                break;
            default:
                $this->result($result, -1, $thecancel['msg']);
                break;
        }
    }


}
